import type { ResponseData } from '@/utils/request';
import { SUCCESS_CODE } from '@/utils/request';
import type { RequestData } from '@ant-design/pro-table';
import { message } from 'antd';
import { request } from 'umi';
import type {
  createParkItem,
  Has_exists,
  imageList,
  ParkItem,
  QueryParams,
  updateParkItem,
} from './types';

//查询单个数据
export async function getParkDetail(id: string) {
  const res = await request(`/zone/zone/${id}`);
  return res;
}

//停车场列表
export async function getParktable(params: QueryParams): Promise<RequestData<ParkItem>> {
  const { current, ...param } = params;

  const res = await request('/zone/zone/list', {
    params: {
      page: params.current,
      pageSize: params.pageSize,
      ...param,
    },
  });
  if (!res || res.code !== SUCCESS_CODE) {
    message.error('获取失败');
    return { success: false, total: 0, data: [] };
  }
  return {
    success: true,
    total: res.data.pagination.total,
    data: res.data.list,
  };
}

//删除停车场
export async function deletePark(params: { id: string }): Promise<void> {
  const res: ResponseData<ParkItem> = await request(`/zone/zone/${params.id}`, {
    method: 'DELETE',
  });

  if (res.code !== SUCCESS_CODE) {
    throw new Error(res.message);
  }
}

//添加停车场
export async function addParkItem(params: createParkItem) {
  let district: string = '';
  if (params?.district && params?.district?.length > 0) {
    district = params?.district[params?.district.length - 1];
  }
  const regex = /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2})/;
  const res: ResponseData<ParkItem> = await request('/zone/zone/add', {
    method: 'POST',
    data: {
      ...params,
      // space_total_count: params.space_total_count || 0,
      lat: params.location?.lat || 0.0,
      lng: params.location?.lng || 0.0,
      city_code: district || '',
      scheme_id: params.scheme?.id,
      open_time: params.open_time
        ?.map((item: any) => {
          regex.test(item);
          const time = RegExp.$4 + ':' + RegExp.$5;
          return time;
        })
        .join('-'),
      reserve_scheme_id: params.reserve_scheme?.id,
    },
  });
  return res;
}

//修改停车场

export async function UpdateParkItem(id: string, params: updateParkItem) {
  let district: string = '';
  if (typeof params?.district == 'string') {
    params.district = undefined;
  }
  if (params?.district && params?.district?.length > 0) {
    district = params?.district[params?.district.length - 1];
  }
  const regex = /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2})/;
  const res: ResponseData<ParkItem> = await request('/zone/zone/update', {
    method: 'POST',
    data: {
      ...params,
      // space_total_count: params.space_total_count,
      id: id,
      lat: params.location?.lat,
      lng: params.location?.lng,
      city_code: district || '',
      scheme_id: params.scheme?.id,
      open_time: params.open_time
        ?.map((item: any) => {
          regex.test(item);
          const time = RegExp.$4 + ':' + RegExp.$5;
          return time;
        })
        .join('-'),
      reserve_scheme_id: params.reserve_scheme?.id,
    },
  });

  return res;
}

//获取停车场名称
export async function judgeHas_exists(
  zone_name: string | undefined,
  excludeId?: string | undefined,
): Promise<Has_exists> {
  const res = await request('/zone/zone/has_exists', {
    params: { zone_name: zone_name, excludeId: excludeId || '1' },
  });
  return res;
}

//获取车场图片数据
export async function getImage(zoneId: string): Promise<Has_exists> {
  const res = await request(`/zone/${zoneId}/picture`);
  return res;
}

//更新车场图片数据
export async function updateImage(params: imageList): Promise<Has_exists> {
  console.log(params.urls);
  const res = await request(`/zone/${params.zoneId}/picture`, {
    method: 'POST',
    data: { urls: params.urls },
  });
  return res;
}
